<?php

class Activity_Widget_Browse_Profile_Feed extends Nuo_Widget
{
	function execute()
	{
		$activityApi = Nuo::getApi('activity');

		//$activityApi->getFeed();

		$mainResource = Nuo::getVar('mainResource');

		$table = Nuo::getTable('activity/feed');

		$rows = $activityApi -> getItemFeed($mainResource);

		$items = array();

		$arrId = array();

		foreach ($rows as $row)
		{
			$arrId[] = $id = $row -> getItemId();
			$items[$id] = $row;
		}

		$poster = Nuo::getUser();

		if ($poster->isGuest())
		{
			return '';
		}

		$posterId = $poster -> getItemId();
		
		if ($arrId && $posterId)
		{
			$table = Nuo::getTable('activity/like');
			$select = $table -> select() -> where('resource_id IN (?)', $arrId) -> where('poster_id=?', $posterId);

			foreach ($table->fetchAll($select) as $like)
			{
				$id = $like -> object_id;
				if (isset($items[$id]))
				{
					$obj = $items[$id];
					$obj -> isLiked = $like -> is_like;
				}
			}
		}
		
		$viewer = Nuo::getUser();
		
		$isUser = is_object($viewer);

		return $this -> render(array(
			'resource' => $mainResource,
			'items' => $items,
			'viewer' => $viewer,
			'isUser' => $isUser,
		));
	}

}
